/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.accountinfo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfo;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfoAddress;
import com.thinkgem.jeesite.modules.accountinfo.service.AccountInfoAddressService;
import com.thinkgem.jeesite.modules.accountinfo.service.AccountInfoService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 添加地址Controller
 * @author ljh
 * @version 2016-12-15
 */
@Controller
@RequestMapping(value = "${adminPath}/address/accountInfoAddress")
public class AccountInfoAddressController extends BaseController {
    
    @Autowired
    private AccountInfoService accountInfoService;

	@Autowired
	private AccountInfoAddressService accountInfoAddressService;
	
	@ModelAttribute
	public AccountInfoAddress get(@RequestParam(required=false) String id) {
		AccountInfoAddress entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = accountInfoAddressService.get(id);
		}
		if (entity == null){
			entity = new AccountInfoAddress();
		}
		return entity;
	}
	
	@RequiresPermissions("address:accountInfoAddress:view")
    @RequestMapping(value = {"index"})
    public String index(User user, Model model) {
        return "modules/account/address/accountInfoIndex";
    }
	
	@RequiresPermissions("address:accountInfoAddress:view")
	@RequestMapping(value = {"list", ""})
	public String list(AccountInfoAddress accountInfoAddress,String username, HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes,Model model) throws Exception {
	    if(username!=null&&!"".equals(username)){
	        AccountInfo user = new AccountInfo();
	        user.setUsername(username);
	        user = accountInfoService.validateUser(user);
	        if(user==null){
                addMessage(redirectAttributes, "无此用户");
                return "modules/account/address/accountInfoAddressList"; 
            }
	        accountInfoAddress.setAccountInfo(user);
	    }
		Page<AccountInfoAddress> page = accountInfoAddressService.findPage(new Page<AccountInfoAddress>(request, response), accountInfoAddress); 
		model.addAttribute("page", page);
		if(accountInfoAddress.getAccountInfo().getId()!=null){
		    model.addAttribute("accountInfo", accountInfoService.get(accountInfoAddress.getAccountInfo().getId()));
		}
		return "modules/account/address/accountInfoAddressList";
	}
	
	@RequiresPermissions("address:accountInfoAddress:view")
	@RequestMapping(value = "form")
	public String form(AccountInfoAddress accountInfoAddress, Model model) {
	    accountInfoAddress.setAccountInfo(accountInfoService.get(accountInfoAddress.getAccountInfo().getId()));
		model.addAttribute("accountInfoAddress", accountInfoAddress);
		return "modules/account/address/accountInfoAddressForm";
	}

	@RequiresPermissions("address:accountInfoAddress:edit")
	@RequestMapping(value = "save")
	public String save(AccountInfoAddress accountInfoAddress, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, accountInfoAddress)){
			return form(accountInfoAddress, model);
		}
		accountInfoAddressService.save(accountInfoAddress);
		addMessage(redirectAttributes, "保存地址管理成功");
		return "redirect:"+Global.getAdminPath()+"/address/accountInfoAddress/?repage";
	}
	
	@ResponseBody
    @RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeDate(AccountInfo accountInfo,HttpServletResponse response){
	    response.setContentType("application/json; charset=UTF-8");
	    List<AccountInfo> accountinfoList = accountInfoService.findList(accountInfo);
	    List<Map<String,Object>> mapList  =Lists.newArrayList();
	    for(int i=0;i<accountinfoList.size();i++){
	        AccountInfo ainfo = accountinfoList.get(i);
	        Map<String,Object> map = new HashMap<String,Object>();
	        map.put("id", ainfo.getId());
	        map.put("name", ainfo.getUsername());
	        mapList.add(map);
	    }
	    return mapList;
	}
	
	@RequiresPermissions("address:accountInfoAddress:edit")
	@RequestMapping(value = "delete")
	public String delete(AccountInfoAddress accountInfoAddress, RedirectAttributes redirectAttributes) {
		accountInfoAddressService.delete(accountInfoAddress);
		addMessage(redirectAttributes, "删除地址管理成功");
		return "redirect:"+Global.getAdminPath()+"/address/accountInfoAddress/?repage";
	}

}